<?php
    /**
     * Created by PhpStorm.
     * User: zgh
     * Date: 2021-07-21
     * Time: 10:10
     */

    namespace app\common\model;


    use think\Model;

    class SystemNodeModel extends Model
    {
        protected $autoWriteTimestamp = true;

        const tableName = 'system_node';

        public static function getMagicTableName()
        {
            return '__' . strtoupper(self::tableName) . '__';
        }

        public function getCreateTimeAttr($value)
        {
            return date("Y-m-d H:i:s",$value);
        }



        public function getNodeTreeList()
        {
            $list = $this->select()->toArray();
            $list = $this->buildNodeTree($list);
            return $list;
        }

        protected function buildNodeTree($list)
        {
            $newList = [];
            $repeatString = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            foreach ($list as $vo) {
                if ($vo['type'] == 1) {
                    $newList[] = $vo;
                    foreach ($list as $v) {
                        if ($v['type'] == 2 && strpos($v['node'], $vo['node'] . '/') !== false) {
                            $v['node'] = "{$repeatString}├{$repeatString}" . $v['node'];
                            $newList[] = $v;
                        }
                    }
                }
            }
            return $newList;
        }

    }